package com.canxin.controller;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import com.alibaba.fastjson.JSONArray;
import com.canxin.entity.ScoreEntity;
import com.canxin.entity.StudentsEntity;
import com.canxin.service.BinduserServiceI;
import com.canxin.service.ScoreServiceI;
import com.canxin.service.StudentServiceI;

import net.sf.json.JSONObject;
import weixin.guanjia.account.service.WeixinAccountServiceI;
import weixin.guanjia.core.util.WeixinUtil;
import weixin.p3.oauth2.rule.RemoteWeixinMethod;
import weixin.util.WeiXinConstants;

/**   
 * @Title: Controller
 * @Description: 成绩信息表
 * @author zhangdaihao
 * @date 2017-03-05 22:59:42
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/scoreController")
public class ScoreController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ScoreController.class);

	@Autowired
	private ScoreServiceI scoreService;
	@Autowired
	private BinduserServiceI binduserService;
	@Autowired
	private StudentServiceI studentService;
	@Autowired
	private RemoteWeixinMethod remoteWeixinMethod;

	@Autowired
	private SystemService systemService;
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@ResponseBody
	@RequestMapping(params = "getScoreListByAuthor2")
	public void getScoreListByAuthor2(HttpServletRequest request,HttpServletResponse resp) throws IOException
	{
		String returnUrl = remoteWeixinMethod.callWeixinAuthor2ReturnUrl(request,"gh_d1ace5896840","http://canxin.mynatapp.cc/mobile/page/historyExamList.html");
		if(returnUrl==null)
			returnUrl="http://canxin.mynatapp.cc/mobile/page/historyExamList.html";
		resp.sendRedirect(returnUrl);
	}

	@ResponseBody
	@RequestMapping(params = "getScoreListByOpenId",produces="application/json;charset=UTF-8")
	public void scoreList(HttpServletRequest request,HttpServletResponse resp) throws IOException
	{
		remoteWeixinMethod.callWeixinAuthor2ReturnUrl(request,"gh_d1ace5896840","http://canxin.mynatapp.cc/mobile/page/historyExamList.html");
		String openId =(String) request.getSession().getAttribute(WeiXinConstants.USER_OPENID);
		String studentId = binduserService.getStudentIdByOpenId(openId);
		List<ScoreEntity> scoreEntityList = scoreService.getAllScoresOrderByExamDate(studentId);
		JSONArray jsonArray = new JSONArray();
		for (ScoreEntity scoreEntity : scoreEntityList) {
			jsonArray.add(scoreEntity);
		}
		resp.getWriter().write(jsonArray.toJSONString());
		resp.getWriter().flush();
		resp.getWriter().close();
	}

	@ResponseBody
	@RequestMapping(params = "getScoreListByExamId",produces="application/json;charset=UTF-8")
	public AjaxJson getScoreListByExamId(@RequestParam("examId") String examId) throws IOException
	{
		AjaxJson result = new AjaxJson();
		List l = scoreService.getAllStudentScoresByExamId(examId);
		result.setObj(l);
		return result;
	}

	@ResponseBody
	@RequestMapping(params = "getNextStudentExamList",produces="application/json;charset=UTF-8")
	public void getNextStudentExamList(HttpServletRequest request,HttpServletResponse resp,@RequestParam("studentId") String studentId) throws IOException {
		logger.info(studentId);
		List<StudentsEntity> studentsEntityList = studentService.getAllStudents();
		JSONArray jsonArray = new JSONArray();
		for (int i =0;i<studentsEntityList.size();i++) {
			StudentsEntity student = studentsEntityList.get(i);
			if(i==studentsEntityList.size()-1)
			{
				jsonArray.add(scoreService.getAllScoresOrderByExamDate(studentsEntityList.get(0).getId()));
				break;
			}
			if(student.getId().equals(studentId))
			{
				jsonArray.add(scoreService.getAllScoresOrderByExamDate(studentsEntityList.get(i+1).getId()));
				break;
			}
		}
		System.out.println(jsonArray.toJSONString());
		resp.getWriter().write(jsonArray.toJSONString());
		resp.getWriter().flush();
		resp.getWriter().close();
	}


	@ResponseBody
	@RequestMapping(params = "scoreList",produces="application/json;charset=UTF-8")
	public AjaxJson scoreList(HttpServletRequest request,HttpServletResponse resp,@RequestParam("studentId") String studentId
		) throws IOException {
		logger.info(studentId);
		List<ScoreEntity> scoreEntityList = scoreService.getAllScoresOrderByExamDate(studentId);
		JSONArray jsonArray = new JSONArray();
		for (ScoreEntity scoreEntity : scoreEntityList) {
			jsonArray.add(scoreEntity);
		}
		AjaxJson result = new AjaxJson();
		result.setObj(jsonArray);
		return result;
	}

	/**
	 * 成绩信息表列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "score")
	public ModelAndView score(HttpServletRequest request) {
		
		return new ModelAndView("com/canxin/score/scoreList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ScoreEntity score,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ScoreEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, score, request.getParameterMap());
		this.scoreService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

}
